package com.sanjishu.enterlab.electricity.sale.utils;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Map;

public class WordUtils {


    public static void main(String args[]) throws Exception {

    }

    public static void createWord(String templateName, String outputFile, Map<String, String> dataMap) throws Exception {
        // 获取模板文件
        File templateFile = new File(getPath()+templateName);
        FileInputStream in = new FileInputStream(templateFile);
        HWPFDocument hwpfDocument = new HWPFDocument(in);
        Range range = hwpfDocument.getRange();
        for(Map.Entry<String,String> entry:dataMap.entrySet()){
            String value = entry.getValue();
            if (value == null) {
                value="";
            }
            range.replaceText(entry.getKey(), value);
        }

        OutputStream ostream = new FileOutputStream(new File(outputFile));
        hwpfDocument.write(ostream);
        ostream.flush();
        ostream.close();
    }

    public static String getPath(){
        return WordUtils.class.getClassLoader().getResource("/").getPath();
    }
}
